const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
//const OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin')
const TerserWebpackPlugin = require('terser-webpack-plugin') //因为你的代码里面包含了ES6的语法，你的uglifyjs不支持的呀
module.exports = {
  entry: './src/main.js',
  output: {
    filename: '[name]-[contenthash:8].js',
    path: path.join(__dirname, 'dist'),
  }, 
  optimization: {
    sideEffects: true,
    // 模块只导出被使用的成员
     usedExports: true,
    // 尽可能合并每一个模块到一个函数中
     concatenateModules: true,
     minimize: true,
     minimizer: [
      new TerserWebpackPlugin(),
     // new OptimizeCssAssetsWebpackPlugin()
    ]
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      },
      {
        test: /\.js$/,
        loader: 'babel-loader'
      },
      {
        test: /\.(vue|js)$/,
        exclude: /node_modules/,
        loader: 'eslint-loader',
        enforce: 'pre',
      },
      {
        test: /\.css$/,
        use: [
          // MiniCssExtractPlugin.loader,
          'style-loader',
          'css-loader'
        ]
      },
      {
        test:/\.less$/,
        use: [
            //  MiniCssExtractPlugin.loader,       
             'style-loader',
             'css-loader',
             'less-loader'
        ]
    },
    {
      test: /\.(png|jpe?g|gif|svg)$/,
      use: {
        loader: 'url-loader',
        options: {
          esModule: false, // 不加的话会有这种情况 img属性src="[object Module]"
          limit: 1024 * 10, // 当大于10kb时候，将文件打包到publicPath中 
          outputPath: 'assets', // 将文件打包到哪里
         // publicPath: 'images/',
          name: '[name].[hash:8].[ext]'

        } 
      }
     }
    ]
  },
  plugins: [
    new VueLoaderPlugin(),
    // 用于生成 index.html
    new HtmlWebpackPlugin({
     title: 'Webpack Plugin Sample',
     meta: {
       viewport: 'width=device-width'
     },
     template: './public/index.html',
     templateParameters: {
       BASE_URL: './'
   },
}),
// new MiniCssExtractPlugin({
//   filename: '[name]-[contenthash:8].bundle.css'
// })

  ]
}
